import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    //登录
    path:'/',
    name:'login',
    component: () => import('@/components/Login/Login.vue')

  },

  {
    //公司管理
    path:'/company',
    name:'company',
    component: () => import('@/components/Company/Company.vue'),
    meta: { requiresAuth: true }
  },
  {
    //仓库管理
    path: '/store',
    name: 'store',
    component: () => import('@/components/Store/Store.vue'),
    meta: { requiresAuth: true }
  },

  {
    //入库管理
    path: '/instore',
    name: 'instore',
    component: () => import('@/components/InStore/InStore.vue'),
    meta: { requiresAuth: true }
  },
  {
    //出库管理
    path: '/outstore',
    name: 'outstore',
    component: () => import('@/components/OutStore/OutStore.vue'),
    meta: { requiresAuth: true }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})



router.beforeEach((to, from, next) => {
  const loggedIn = localStorage.getItem('loggedIn'); // 假设使用本地存储来保存登录状态

  if (to.matched.some((record) => record.meta.requiresAuth)) {
    // 如果路由需要权限验证
    if (!loggedIn) {
      // 未登录，跳转到登录页
      next({ name: 'login' });
    } else {
      // 已登录，继续导航到目标页面
      next();
    }
  } else {
    // 不需要权限验证，直接导航到目标页面
    next();
  }
});


export default router
